// 我的我的/delivery_entry/delivery_entry.js
Page({
  data: {
    trackingNumber: '',
    pickupCode: '',
    selectedDateTime: '',
    contactName: '',
    contactPhone: '',
    contactAddress: '',
    contactRemark: ''
  },

  onLoad: function() {
    // 设置默认日期时间为当前时间后30分钟
    const now = new Date();
    now.setMinutes(now.getMinutes() + 30);
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    const hour = now.getHours().toString().padStart(2, '0');
    const minute = now.getMinutes().toString().padStart(2, '0');
    const defaultDateTime = `${year}-${month}-${day} ${hour}:${minute}`;

    this.setData({
      selectedDateTime: defaultDateTime
    });
  },

  // 输入单号
  inputTrackingNumber(e) {
    this.setData({
      trackingNumber: e.detail.value
    });
  },

  // 扫码按钮事件
  scanCode() {
    // 调用微信扫码API
    wx.scanCode({
      success: (res) => {
        this.setData({
          trackingNumber: res.result
        });
      },
      fail: (err) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  },

  // 输入取件码
  inputPickupCode(e) {
    this.setData({
      pickupCode: e.detail.value
    });
  },

  // 日期时间选择
  bindDateTimeChange(e) {
    this.setData({
      selectedDateTime: e.detail.value
    });
  },

  // 提交表单
  submitForm() {
    const { trackingNumber, pickupCode, selectedDateTime, contactName, contactPhone, contactAddress } = this.data;

    // 验证必填项
    if (!trackingNumber) {
      wx.showToast({
        title: '请输入单号',
        icon: 'none'
      });
      return;
    }

    if (!pickupCode) {
      wx.showToast({
        title: '请输入取件码',
        icon: 'none'
      });
      return;
    }

    if (!selectedDateTime) {
      wx.showToast({
        title: '请选择预约时间',
        icon: 'none'
      });
      return;
    }

    if (!contactName) {
      wx.showToast({
        title: '请输入联系人姓名',
        icon: 'none'
      });
      return;
    }

    if (!contactPhone || contactPhone.length !== 11) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      });
      return;
    }

    if (!contactAddress) {
      wx.showToast({
        title: '请输入家庭住址',
        icon: 'none'
      });
      return;
    }

    // 构建预约信息
    const appointmentInfo = {
      trackingNumber,
      pickupCode,
      deliveryTime: selectedDateTime,
      contactName,
      contactPhone,
      contactAddress,
      contactRemark: this.data.contactRemark
    };

    // 这里可以添加提交到服务器的逻辑
    wx.showModal({
      title: '提交成功',
      content: `您已成功提交配送信息，单号：${appointmentInfo.trackingNumber}，预约时间：${appointmentInfo.deliveryTime}，联系人：${appointmentInfo.contactName}，电话：${appointmentInfo.contactPhone}`,
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
          wx.navigateBack();
        }
      }
    });
  },

  // 输入联系人姓名
  inputContactName(e) {
    this.setData({
      contactName: e.detail.value
    });
  },

  // 输入手机号码
  inputContactPhone(e) {
    this.setData({
      contactPhone: e.detail.value
    });
  },

  // 输入家庭住址
  inputContactAddress(e) {
    this.setData({
      contactAddress: e.detail.value
    });
  },

  // 输入备注要求
  inputContactRemark(e) {
    this.setData({
      contactRemark: e.detail.value
    });
  },

  // 返回上一页
  navigateBack() {
    wx.navigateBack();
  }
});